package codec

import (
	"io"
	"reflect"
	"sync"
)

type Header struct {
	ServiceMethod string // 服务.方法名
	Seq           uint64 // 请求序列号

	Error string // 错误信息
}

type request struct {
	h            *Header
	argv, replyv reflect.Value // argv and replyv of request
	mtype        *methodType
	svc          *service

	mux sync.Mutex
}

// Codec 消息编码、接码器
type Codec interface {
	io.Closer
	ReadHeader(*Header) error
	ReadBody(any) error
	Writer(header *Header, data any) error
}

type NewCodeFunc func(writer io.ReadWriteCloser) Codec

type CodecType string

const (
	GobType  CodecType = "application/gob"
	JsonType CodecType = "application/json"
)

var NewCodecFuncMap map[CodecType]NewCodeFunc

func init() {
	NewCodecFuncMap = make(map[CodecType]NewCodeFunc)
	NewCodecFuncMap[GobType] = NewGobCodec
}
